// pages/chudanda/chudanda.js
Page({  
  data: {
    questions: [],  // 存储题目列表  
  },  

  onLoad(options) { 
    //console.log('接收到的参数:', options);  
      this.id = options.id; // 获取试卷 ID   

      this.fetchPaperDetails(this.id); 
      
  },  
  onShow() {   
    // 页面每次显示时请求数据  
    if (this.id) { // 只有在 id 有值时才请求  
      this.fetchPaperDetails(this.id);  
    }  
  },

  fetchPaperDetails(id) {  
    const that = this;   
    const app = getApp();                   
    wx.request({  
      method: 'POST',  
      url: `${app.globalData.baseUrl}/paper/paper-detail`, 
      header: {  
            'authentication': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MzI1NTg3MTcsInVzZXJJZCI6MTgyNzk5OTgyMTczMTA5ODYyNn0.SxS7ENOxgqgIh8AAn_Sw84R_zvFA99_khzsVNCDShLk'  
               },
       data: {
                id:id
               },
      success(res) {  
        //console.log('请求成功:', res.data)
        if (res.statusCode === 200) {  
          const { code, data } = res.data;  
          if (code === 1 && data) {  
            that.setData({  
              questions: data.radioList,
            });  
            // 将题目列表缓存到本地  
            wx.setStorageSync(`papers`, data.radioList);
          } else {  
            console.error('获取试卷详情失败，数据格式不正确:', res.data);  
          }  
        } else {  
          console.error('请求失败:', res);  
        }  
      },  
      fail(err) {  
        console.error('请求失败:', err);  
      }  
    });  
  }, 
  
onQuestionClick(event) {
    const index = event.currentTarget.dataset.index; // 获取题目下标
    const paperId = this.data.questions[index].paperId; // 获取对应的 paperId
    const quId = this.data.questions[index].quId; // 获取对应的 quId
    
    // 跳转到 chudan 页面，并传递 paperId 和 quId
    wx.navigateTo({
      url: `/pages/chudan/chudan?paperId=${paperId}&quId=${quId}&index=${index}`
    });
  },

  onGoackhome() { 
    wx.showModal({  
      title: '提示',  
      content: '是否要退出答题',  
      success: (res) => {   
          if (res.confirm) {  
      wx.switchTab({  
        url: `/pages/home/home`  
    });  
      console.log('用户点击确定');  
  } else if (res.cancel) {  
      console.log('用户点击取消');  
  } 
      },  
  });  
    
    
},  
});